package com.qf.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author 千锋健哥
 */
public class EncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("========初始化过滤器1, init方法执行========");


    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("=========过滤器1====业务方法执行====");
        //1. 设置响应编码以及, 返回数据类型
        servletResponse.setContentType("text/html; charset=utf-8");
        //2. 设置请求编码
        servletRequest.setCharacterEncoding("utf-8");

        //如果有下一个过滤器执行下一个过滤器, 如果没有下一个过滤器, 那么当前过滤器放行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("========初始化过滤器1, destroy方法执行========");

    }

}
